﻿#include "ConquestArcher.h"
#include "../../MirEnvir/Envir.h"
#include "../../MirEnvir/Map.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    bool ConquestArcher::getCanMove() const
    {
        return Route.size() > 0 && !Dead && getEnvir()->getTime() > MoveTime && getEnvir()->getTime() > ActionTime && getEnvir()->getTime() > ShockTime;
    }

    ConquestArcher::ConquestArcher(MonsterInfo *info) : TownArcher(info)
    {
    }

    bool ConquestArcher::IsAttackTarget(MonsterObject *attacker)
    {
        return false;
    }

    void ConquestArcher::Die()
    {
        TownArcher::Die();
    }

    void ConquestArcher::FindTarget()
    {
        for (int d = 0; d <= Info->ViewRange; d++)
        {
            for (int y = getCurrentLocation()->Y - d; y <= getCurrentLocation()->Y + d; y++)
            {
                if (y < 0)
                {
                    continue;
                }
                if (y >= getCurrentMap()->Height)
                {
                    break;
                }

                for (int x = getCurrentLocation()->X - d; x <= getCurrentLocation()->X + d; x += std::abs(y - getCurrentLocation()->Y) == d ? 1 : d * 2)
                {
                    if (x < 0)
                    {
                        continue;
                    }
                    if (x >= getCurrentMap()->Width)
                    {
                        break;
                    }

                    Cell *cell = getCurrentMap()->GetCell(x, y);
                    if (!cell->getValid() || cell->Objects.empty())
                    {
                        continue;
                    }

                    for (int i = 0; i < cell->Objects.size(); i++)
                    {
                        MapObject *ob = cell->Objects[i];
                        switch (ob->getRace())
                        {
                            case ObjectType::Player:
                            {
                                PlayerObject *playerob = static_cast<PlayerObject*>(ob);
                                if (!ob->IsAttackTarget(this))
                                {
                                    continue;
                                }
                                if (playerob->MyGuild != nullptr && playerob->MyGuild->Conquest != nullptr && Conquest->Info->Index == playerob->MyGuild->Conquest->Info->Index || ob->getHidden() && (!CoolEye || getLevel() < ob->getLevel()) || !Conquest->getWarIsOn())
                                {
                                    continue;
                                }
                                setTarget(ob);
                                return;
                            }
                            default:
                                continue;
                        }
                    }
                }
            }
        }
    }

    int ConquestArcher::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        if (!Conquest->getWarIsOn() || attacker->MyGuild != nullptr && Conquest->Owner == attacker->MyGuild->Guildindex)
        {
            damage = 0;
        }

        return TownArcher::Attacked(attacker, damage, type, damageWeapon);
    }

    int ConquestArcher::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        if (!Conquest->getWarIsOn())
        {
            damage = 0;
        }

        return TownArcher::Attacked(attacker, damage, type);
    }
}
